﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace FooFun.DataElements.Dtos
{
    public class ListDataElement : DataElement
    {
        IList list;
        public ListDataElement(IList list)
            : base(list)
        {
        }

        public override object Value
        {
            get
            {
                return list;
            }
            set
            {
                list = (IList)value;
            }
        }

        public override IEnumerator GetEnumerator()
        {
            foreach (object obj in list)
            {
                if (obj is IDataElement)
                {
                    yield return (IDataElement)obj;
                }
                else
                {
                    yield return new ObjectDataElement(obj);
                }
            }
        }

        public override IDataElement this[int index]
        {
            get
            {
                return new ObjectDataElement(list[index]);
            }
            set
            {
                list[index] = value.Value;
            }
        }

        public override IDataElement Add(IDataElement de)
        {
            list.Add(de.Value);
            de.Parent = this;
            return de;
        }

        public override bool IsEmpty
        {
            get
            {
                if (list != null)
                {
                    return list.Count == 0;
                }
                return true;
            }
        }

    }

   
}
